package com.neusoft.rental.service.impl;

import com.neusoft.rental.mapper.GoodsMapper;
import com.neusoft.rental.mapper.GoodsappointmentMapper;
import com.neusoft.rental.mapper.InformationMapper;
import com.neusoft.rental.po.*;
import com.neusoft.rental.service.GoodsAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GoodsAppointmentServiceImpl implements GoodsAppointmentService {

    @Autowired
    private GoodsappointmentMapper goodsappointmentMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private InformationMapper informationMapper;

    //增加物资申请
    @Override
    public int addGoodsAppointment(Goodsappointment goodsappointment) {
        return goodsappointmentMapper.addGoodsappointment(goodsappointment);
    }

    //用户查询已经审核通过的物资申请
    //管理员查询物资申请,若申请预约已经到了开始时间管理员还未通过申请，则默认拒绝，发送预约失败的消息，并删除该申请信息。
    @Override
    public List<Goodsappointment> getGoodsappointment(Goodsappointment goodsappointment) {
        //管理员查询物资申请
        if(goodsappointment.getJudge()!=null){
            System.out.println(goodsappointment.getJudge());
            Date date=new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datenow=formatter.format(date);
            List<Goodsappointment> gList=goodsappointmentMapper.getGoodsappointment(goodsappointment);
            List<Goodsappointment> gList1=new ArrayList<Goodsappointment>();

            for(Goodsappointment g:gList){
                if(datenow.compareTo(g.getStarttime())>0 && g.getJudge()!=2){
                    Goods goods=goodsMapper.queryGoodsById(g.getGoodsid());
                    Information information=new Information(null,g.getUserid(),
                            "系统提醒",goods.getGoodsname()+"预约失败," +
                            "管理员超时未处理，请另选时间预约",datenow,0);
                    informationMapper.addInformation(information);
                    goodsappointmentMapper.deleteGoodsappointment(g);
                }
                else{
                    gList1.add(g);
                }
            }
            return gList1;
        }
        //用户查询物资申请
        else{
            return goodsappointmentMapper.getGoodsappointment(goodsappointment);
        }
    }

    //用户归还物资设置finish
    //更新对应预约信息中的judge,judge为1表示院级审核同意预约信息，judge为2表示校级同意，审核完成同时给用户发送消息
    //审核完成同时改变对应物资的库存数量，审核不通过删除申请信息。
    @Override
    public int judgeGoodsAppointment(Goodsappointment goodsappointment) {
        //管理员审核物资申请
        if (goodsappointment.getJudge() != null) {
            //通过goodsid查找对应goods，短信发送需要用到goodsname
            Goods goods = goodsMapper.queryGoodsById(goodsappointment.getGoodsid());
            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String datenow = formatter.format(date);

            Information information1 = new Information(null, goodsappointment.getUserid(),
                    "系统提醒", goods.getGoodsname() + "预约审核成功", datenow, 0);
            Information information2 = new Information(null, goodsappointment.getUserid(),
                    "系统提醒", goods.getGoodsname() + "预约失败,请另选物资或时间预约", datenow, 0);

            //如果校级领导同意，那么场地预约成功，发送系统消息且更新goods表中的goodsnum库存数量
            if (goodsappointment.getJudge() == 2) {
                Goods goods1 = new Goods();
                goods1.setGoodsnum(0-goodsappointment.getRentnum());
                goods1.setGoodsid(goodsappointment.getGoodsid());
                goodsMapper.updateGoods(goods1);
                informationMapper.addInformation(information1);
                return goodsappointmentMapper.judgeGoodsappointment(goodsappointment);
            } else if (goodsappointment.getJudge() == 1) {
                return goodsappointmentMapper.judgeGoodsappointment(goodsappointment);
            } else {
                //如果院级领导或者校级领导审核不通过，judge为0，则发送预约失败的消息，并删除该预约记录。
                informationMapper.addInformation(information2);
                return goodsappointmentMapper.deleteGoodsappointment(goodsappointment);
            }
        }
        //用户归还物资,对应物资库存也会发生变化
        else {
            //修改物资库存
            System.out.println(goodsappointment.getRentnum());
            Goods goods1 = new Goods();
            goods1.setGoodsnum(goodsappointment.getRentnum());
            goods1.setGoodsid(goodsappointment.getGoodsid());
            goodsMapper.updateGoods(goods1);
            return goodsappointmentMapper.judgeGoodsappointment(goodsappointment);
        }
    }
}
